package com.example.teaching_server.controller;

import com.example.teaching_server.common.ApiResponse;
import com.example.teaching_server.dto.FileUploadResponse;
import com.example.teaching_server.service.FileUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/api/upload")
@RequiredArgsConstructor
@Tag(name = "文件上传管理", description = "文件上传相关接口")
public class FileUploadController {

    private final FileUploadService fileUploadService;

    /**
     * 通用文件上传
     */
    @PostMapping("/file")
    @Operation(summary = "上传文件", description = "通用文件上传接口")
    public ApiResponse<FileUploadResponse> uploadFile(
            @Parameter(description = "上传的文件")
            @RequestParam("file") MultipartFile file) {

        log.info("开始上传文件: fileName={}, size={} bytes", file.getOriginalFilename(), file.getSize());

        try {
            FileUploadResponse response = fileUploadService.uploadFile(file);

            if (response.isSuccess()) {
                log.info("文件上传成功: fileName={}", file.getOriginalFilename());
                return ApiResponse.success(response.getMessage(), response);
            } else {
                log.warn("文件上传失败: fileName={}, message={}", file.getOriginalFilename(), response.getMessage());
                return ApiResponse.fail(response.getMessage());
            }
        } catch (Exception e) {
            log.error("文件上传异常: fileName={}", file.getOriginalFilename(), e);
            return ApiResponse.fail("文件上传失败: " + e.getMessage());
        }
    }

    /**
     * 图片上传
     */
    @PostMapping("/image")
    @Operation(summary = "上传图片", description = "图片文件上传接口")
    public ApiResponse<FileUploadResponse> uploadImage(
            @Parameter(description = "上传的图片")
            @RequestParam("image") MultipartFile image) {

        log.info("开始上传图片: fileName={}, size={} bytes", image.getOriginalFilename(), image.getSize());

        try {
            FileUploadResponse response = fileUploadService.uploadFile(image);

            if (response.isSuccess()) {
                log.info("图片上传成功: fileName={}", image.getOriginalFilename());
                return ApiResponse.success("图片上传成功", response);
            } else {
                log.warn("图片上传失败: fileName={}, message={}", image.getOriginalFilename(), response.getMessage());
                return ApiResponse.fail(response.getMessage());
            }
        } catch (Exception e) {
            log.error("图片上传异常: fileName={}", image.getOriginalFilename(), e);
            return ApiResponse.fail("图片上传失败: " + e.getMessage());
        }
    }

    /**
     * 视频上传
     */
    @PostMapping("/video")
    @Operation(summary = "上传视频", description = "视频文件上传接口")
    public ApiResponse<FileUploadResponse> uploadVideo(
            @Parameter(description = "上传的视频")
            @RequestParam("video") MultipartFile video) {

        log.info("开始上传视频: fileName={}, size={} bytes", video.getOriginalFilename(), video.getSize());

        try {
            FileUploadResponse response = fileUploadService.uploadFile(video);

            if (response.isSuccess()) {
                log.info("视频上传成功: fileName={}", video.getOriginalFilename());
                return ApiResponse.success("视频上传成功", response);
            } else {
                log.warn("视频上传失败: fileName={}, message={}", video.getOriginalFilename(), response.getMessage());
                return ApiResponse.fail(response.getMessage());
            }
        } catch (Exception e) {
            log.error("视频上传异常: fileName={}", video.getOriginalFilename(), e);
            return ApiResponse.fail("视频上传失败: " + e.getMessage());
        }
    }
}
